package gttp.nspa.client.adm.ui;

import gttp.nspa.client.adm.activity.ICustomerForm;

import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.util.Format;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent.DialogHideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.info.Info;

public class CustomerForm implements ICustomerForm {

    private TextField               tfId;
    private TextField               tfName;
    private TextField               tfAddr;
    private TextField               tfPhone;
    private TextField               tfEmail;
    private TextField               tfLastVisit;

    private TextButton              btnSave;
    private TextButton              btnDelete;
    private TextButton              btnCancel;

    private VerticalLayoutContainer con;
    private Dialog                  form;

    private ICustomerForm.Presenter presenter;

    public CustomerForm() {
        this("Customer", 350, 75);
    }

    CustomerForm(String caption, int formWidth, int labelWidth) {
        form = createForm(caption, formWidth);
        con = new VerticalLayoutContainer();
        form.add(con);
        createFormFields(con, labelWidth);
        createFormCommands(form);

        btnSave.addSelectHandler(new SelectHandler() {

            public void onSelect(SelectEvent event) {
                presenter.onSaveButtonClicked();
            }
        });
        btnDelete.addSelectHandler(new SelectHandler() {

            public void onSelect(SelectEvent event) {
                presenter.onDeleteButtonClicked();
            }
        });
        btnCancel.addSelectHandler(new SelectHandler() {

            public void onSelect(SelectEvent event) {
                presenter.onCancelButtonClicked();
            }
        });
    }

    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    public HasText id() {
        return tfId;
    }

    public HasText name() {
        return tfName;
    }

    public HasText address() {
        return tfAddr;
    }

    public HasText phone() {
        return tfPhone;
    }

    public HasText email() {
        return tfEmail;
    }

    public void prepareForCreating() {
        btnSave.setText("Create");
        btnSave.setVisible(true);
        btnDelete.setVisible(false);
    }

    public void prepareForUpdating() {
        btnSave.setText("Update");
        btnSave.setVisible(true);
        btnDelete.setVisible(true);
    }

    public void readyForEditing(boolean ready) {
        btnSave.setEnabled(ready);
        btnDelete.setEnabled(ready);
    }

    public void queryDeleting(long id, String name) {
        String msg = Format.substitute("Are you sure to delete: {0}({1})?", name, id);
        MessageBox mb = new MessageBox("Delete?", msg);
        mb.setPredefinedButtons(PredefinedButton.YES, PredefinedButton.NO);
        mb.addDialogHideHandler(new DialogHideHandler() {

            public void onDialogHide(DialogHideEvent event) {
                if (PredefinedButton.YES == event.getHideButton()) {
                    presenter.onDeleteConfirmed();
                }
            }
        });
        mb.show();
    }

    public void exceptionCaught(Throwable t) {
        ExceptionInfo.showInfo(t);
    }

    public void show() {
        form.show();
        form.setFocusWidget(tfName);
    }

    Dialog createForm(String caption, int width) {
        Dialog dialog = new Dialog();
        dialog.setBodyStyle("padding: 15px");
        dialog.setHeadingText(caption);
        dialog.setWidth(width);
        dialog.setShadow(true);
        dialog.setResizable(false);
        dialog.setModal(true);
        dialog.getButtonBar().clear();
        return dialog;
    }

    void createFormFields(VerticalLayoutContainer con, int labelWidth) {
        FieldLabel fl;

        tfId = new TextField();
        tfId.setEnabled(false);
        fl = new FieldLabel(tfId, "Id");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfName = new TextField();
        fl = new FieldLabel(tfName, "Tên");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfAddr = new TextField();
        fl = new FieldLabel(tfAddr, "Địa chỉ");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfPhone = new TextField();
        fl = new FieldLabel(tfPhone, "Điện thoại");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfEmail = new TextField();
        fl = new FieldLabel(tfEmail, "Email");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfLastVisit = new TextField();
        fl = new FieldLabel(tfLastVisit, "Last visit");
        fl.setEnabled(false);
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));
    }

    protected void createFormCommands(ContentPanel pane) {
        btnSave = new TextButton("Save");
        btnDelete = new TextButton("Delete");
        btnCancel = new TextButton("Cancel");

        pane.addButton(btnSave);
        pane.addButton(btnDelete);
        pane.addButton(btnCancel);
    }

    public String toString() {
        return form.getHeader().getText();
    }

    public void customerCreated(long id, String name) {
        Info.display(Format.substitute("{0}({1})", name, id), "Created successfully!");
        form.hide();
    }

    public void customerUpdated(long id, String name) {
        Info.display(Format.substitute("{0}({1})", name, id), "Updated successfully!");
        form.hide();
    }

    public void customerDeleted(long id, String name) {
        Info.display(Format.substitute("{0}({1})", name, id), "Deleted successfully!");
        form.hide();
    }

    public void canceled() {
        form.hide();
    }

    public Widget asWidget() {
        return form;
    }
}
